<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>FedAvg代码 | 1006er的博客</title><meta name="keywords" content="代码学习"><meta name="author" content="wrj"><meta name="copyright" content="wrj"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="联邦学习中的FedAvg代码">
<meta property="og:type" content="article">
<meta property="og:title" content="FedAvg代码">
<meta property="og:url" content="http://example.com/2021/11/02/FedAvg%E4%BB%A3%E7%A0%81/index.html">
<meta property="og:site_name" content="1006er的博客">
<meta property="og:description" content="联邦学习中的FedAvg代码">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/wrj1006er/pic/raw/master/pic/07174a74ea4b6c741b57ab39a26c5fd112c3c0b5.jpg@942w_531h_progressive.webp">
<meta property="article:published_time" content="2021-11-02T12:20:40.000Z">
<meta property="article:modified_time" content="2021-11-03T04:15:27.987Z">
<meta property="article:author" content="wrj">
<meta property="article:tag" content="代码学习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/wrj1006er/pic/raw/master/pic/07174a74ea4b6c741b57ab39a26c5fd112c3c0b5.jpg@942w_531h_progressive.webp"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2021/11/02/FedAvg%E4%BB%A3%E7%A0%81/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-11-03 12:15:27'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><link rel="stylesheet" href="APlayer.min.css"><div id="aplayer"></div><script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js" async></script><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><script src="/live2d-widget/autoload.js"></script><meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/atom.xml" title="1006er的博客" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">19</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page" href="/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/wrj1006er/pic/raw/master/pic/07174a74ea4b6c741b57ab39a26c5fd112c3c0b5.jpg@942w_531h_progressive.webp')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">1006er的博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page" href="/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">FedAvg代码</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-11-02T12:20:40.000Z" title="发表于 2021-11-02 20:20:40">2021-11-02</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-11-03T04:15:27.987Z" title="更新于 2021-11-03 12:15:27">2021-11-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E8%81%94%E9%82%A6%E5%AD%A6%E4%B9%A0/">联邦学习</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="FedAvg代码-分析"><a href="#FedAvg代码-分析" class="headerlink" title="FedAvg代码 分析"></a>FedAvg代码 分析</h1><h3 id="数据的分配"><a href="#数据的分配" class="headerlink" title="数据的分配"></a>数据的分配</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> isIID:</span><br><span class="line">    order = np.arange(self.train_data_size)</span><br><span class="line">    np.random.shuffle(order)</span><br><span class="line">    self.train_data = train_images[order]</span><br><span class="line">    self.train_label = train_labels[order]</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    labels = np.argmax(train_labels, axis=<span class="number">1</span>)</span><br><span class="line">    order = np.argsort(labels)</span><br><span class="line">    self.train_data = train_images[order]</span><br><span class="line">    self.train_label = train_labels[order]</span><br></pre></td></tr></table></figure>
<ul>
<li><p>对于独立同分布的数据，将数据打乱然后分配给客户端</p>
</li>
<li><p>对于非独立同分布的数据，先将数据按照标签进行排序，再分给客户端</p>
<h3 id="选择客户端，模型集成"><a href="#选择客户端，模型集成" class="headerlink" title="选择客户端，模型集成"></a>选择客户端，模型集成</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># num_comm 表示通信次数，此处默认值为1k</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(args[<span class="string">&#x27;num_comm&#x27;</span>]):</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 随机选择一部分Client，全部选择会增大通信量，且实验效果可能会不好</span></span><br><span class="line">    <span class="comment"># clients_in_comm表示每次通讯中随机选择的Client数量</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">#将1到num_of_clients个数随机打乱</span></span><br><span class="line">    order = np.random.permutation(args[<span class="string">&#x27;num_of_clients&#x27;</span>])  </span><br><span class="line">    </span><br><span class="line">    <span class="comment">#取前num_in_comm个数作为选择的服务器,num_in_comm是每一轮选择的服务器个数</span></span><br><span class="line">    clients_in_comm = [<span class="string">&#x27;client&#123;&#125;&#x27;</span>.<span class="built_in">format</span>(i) <span class="keyword">for</span> i <span class="keyword">in</span> order[<span class="number">0</span>:num_in_comm]]</span><br><span class="line">   </span><br><span class="line">    sum_parameters = <span class="literal">None</span></span><br><span class="line">    <span class="comment"># 每个Client基于当前模型参数和自己的数据训练并更新模型，返回每个Client更新后的参数</span></span><br><span class="line">    <span class="keyword">for</span> client <span class="keyword">in</span> tqdm(clients_in_comm):</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 获取当前Client训练得到的参数</span></span><br><span class="line">        local_parameters = myClients.clients_set[client].localUpdate(args[<span class="string">&#x27;epoch&#x27;</span>],     args[<span class="string">&#x27;batchsize&#x27;</span>], net,loss_func, opti, global_parameters)</span><br><span class="line">        </span><br><span class="line">        <span class="comment"># 对所有的Client返回的参数累加（最后取平均值）</span></span><br><span class="line">        <span class="keyword">if</span> sum_parameters <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">            sum_parameters = local_parameters</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">for</span> var <span class="keyword">in</span> sum_parameters:</span><br><span class="line">                sum_parameters[var] = sum_parameters[var] + local_parameters[var]</span><br><span class="line">    <span class="comment"># 取平均值，得到本次通信中Server得到的更新后的模型参数            </span></span><br><span class="line">    <span class="keyword">for</span> var <span class="keyword">in</span> global_parameters:</span><br><span class="line">        global_parameters[var] = (sum_parameters[var] / num_in_comm)   </span><br></pre></td></tr></table></figure>
<h3 id="Client端的训练函数"><a href="#Client端的训练函数" class="headerlink" title="Client端的训练函数"></a>Client端的训练函数</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">localUpdate</span>(<span class="params">self, localEpoch, localBatchSize, Net, lossFun, opti, global_parameters</span>):</span></span><br><span class="line">    <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">        </span></span><br><span class="line"><span class="string">    :param localEpoch: 当前Client的迭代次数</span></span><br><span class="line"><span class="string">    :param localBatchSize: 当前Client的batchsize大小</span></span><br><span class="line"><span class="string">    :param Net: Server共享的模型</span></span><br><span class="line"><span class="string">    :param lossFun: 损失函数</span></span><br><span class="line"><span class="string">    :param opti: 优化函数</span></span><br><span class="line"><span class="string">    :param global_parameters: 当前通信中最新全局参数 </span></span><br><span class="line"><span class="string">    :return: 返回当前Client基于自己的数据训练得到的新的模型参数</span></span><br><span class="line"><span class="string">    &#x27;&#x27;&#x27;</span></span><br><span class="line">    <span class="comment"># 加载当前通信中最新全局参数</span></span><br><span class="line">    Net.load_state_dict(global_parameters, strict=<span class="literal">True</span>)</span><br><span class="line">    <span class="comment"># 载入Client自有数据集</span></span><br><span class="line">    self.train_dl = DataLoader(self.train_ds, batch_size=localBatchSize, shuffle=<span class="literal">True</span>)</span><br><span class="line">    <span class="comment"># 设置迭代次数</span></span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(localEpoch):</span><br><span class="line">        <span class="keyword">for</span> data, label <span class="keyword">in</span> self.train_dl:</span><br><span class="line">            data, label = data.to(self.dev), label.to(self.dev)</span><br><span class="line">            preds = Net(data)</span><br><span class="line">            loss = lossFun(preds, label)</span><br><span class="line">            loss.backward()</span><br><span class="line">            opti.step()</span><br><span class="line">            opti.zero_grad()</span><br><span class="line"> <span class="comment"># 返回当前Client基于自己的数据训练得到的新的模型参数</span></span><br><span class="line">    <span class="keyword">return</span> Net.state_dict()</span><br></pre></td></tr></table></figure>
<h3 id="准确率测试"><a href="#准确率测试" class="headerlink" title="准确率测试"></a>准确率测试</h3><p>利用服务器的模型进行的</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> torch.no_grad():</span><br><span class="line">    <span class="comment"># 加载Server在最后得到的模型参数</span></span><br><span class="line">    net.load_state_dict(global_parameters, strict=<span class="literal">True</span>)</span><br><span class="line">    sum_accu = <span class="number">0</span></span><br><span class="line">    num = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 载入测试集</span></span><br><span class="line">    <span class="keyword">for</span> data, label <span class="keyword">in</span> testDataLoader:</span><br><span class="line">        data, label = data.to(dev), label.to(dev)</span><br><span class="line">        preds = net(data)</span><br><span class="line">        preds = torch.argmax(preds, dim=<span class="number">1</span>)</span><br><span class="line">        sum_accu += (preds == label).<span class="built_in">float</span>().mean()</span><br><span class="line">        num += <span class="number">1</span></span><br><span class="line">    print(<span class="string">&#x27;accuracy: &#123;&#125;&#x27;</span>.<span class="built_in">format</span>(sum_accu / num))</span><br></pre></td></tr></table></figure>
<h3 id="分组"><a href="#分组" class="headerlink" title="分组"></a>分组</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">order = np.random.permutation(<span class="number">10</span>)</span><br><span class="line">temp = np.array([<span class="number">0</span>,<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>,<span class="number">60</span>,<span class="number">70</span>,<span class="number">80</span>,<span class="number">90</span>]) </span><br><span class="line">order+=temp</span><br><span class="line">clients_in_comm = [<span class="string">&#x27;client&#123;&#125;&#x27;</span>.<span class="built_in">format</span>(i) <span class="keyword">for</span> i <span class="keyword">in</span> order[<span class="number">0</span>:num_in_comm]]</span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/wrj1006er/pic/raw/master/pic/5F7CE0F05FD082926693A7F64BE25C87.png" alt="RUNOOB 图标"></p>
</li>
</ul>
<p>按照0-9|10-19|20-29|…….|90-99|进行分组，然后每一轮在每组中随机抽取一个客户端，进行训练。</p>
<h3 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h3><p><img src="https://gitee.com/wrj1006er/pic/raw/master/pic/idd.png" alt="RUNOOB 图标"></p>
<p>数据的idd分布（独立同分布）准确率99.0%</p>
<p><img src="https://gitee.com/wrj1006er/pic/raw/master/pic/non-idd.png" alt="RUNOOB 图标"></p>
<p>数据的non-idd分布（独立同分布）97.7%</p>
<p><img src="https://gitee.com/wrj1006er/pic/raw/master/pic/non-idd(group).png" alt="RUNOOB 图标"></p>
<p>数据的non-idd分布（通过group选取客户端） 97.5%</p>
<h3 id="结论"><a href="#结论" class="headerlink" title="结论"></a>结论</h3><ul>
<li>在数据的独立同分布与非独立同分布情况下不仅准确率更高，并且收敛的速度更快</li>
<li>在分独立同分布情况下利用先按照客户端分组再选择的的方法与不分组准确率几乎一样，但是收敛的数度更快</li>
</ul>
<h3 id="改进点"><a href="#改进点" class="headerlink" title="改进点"></a>改进点</h3><ul>
<li>minst数据集准确率太高导致差距不大，考虑更换数据集,或者更换算法再次进行实验。</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wrj</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2021/11/02/FedAvg%E4%BB%A3%E7%A0%81/">http://example.com/2021/11/02/FedAvg%E4%BB%A3%E7%A0%81/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">1006er的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E4%BB%A3%E7%A0%81%E5%AD%A6%E4%B9%A0/">代码学习</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/wrj1006er/pic/raw/master/pic/07174a74ea4b6c741b57ab39a26c5fd112c3c0b5.jpg@942w_531h_progressive.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/11/08/docker%E5%9F%BA%E7%A1%80/"><img class="prev-cover" src="https://gitee.com/wrj1006er/pic/raw/master/pic/01c10bca6b06d208a5b8e384c8edd9079fac5cb1.jpg@1320w_742h.webp" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">docker基础</div></div></a></div><div class="next-post pull-right"><a href="/2021/11/02/Linux-shell/"><img class="next-cover" src="https://gitee.com/wrj1006er/pic/raw/master/pic/51758dc1b5c9374b313fa68ce510285e7881550f.jpg@942w_531h_progressive.webp" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Linux-shell</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/img/avatar.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">wrj</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">19</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#FedAvg%E4%BB%A3%E7%A0%81-%E5%88%86%E6%9E%90"><span class="toc-number">1.</span> <span class="toc-text">FedAvg代码 分析</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%9A%84%E5%88%86%E9%85%8D"><span class="toc-number">1.0.1.</span> <span class="toc-text">数据的分配</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E5%AE%A2%E6%88%B7%E7%AB%AF%EF%BC%8C%E6%A8%A1%E5%9E%8B%E9%9B%86%E6%88%90"><span class="toc-number">1.0.2.</span> <span class="toc-text">选择客户端，模型集成</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Client%E7%AB%AF%E7%9A%84%E8%AE%AD%E7%BB%83%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.3.</span> <span class="toc-text">Client端的训练函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%86%E7%A1%AE%E7%8E%87%E6%B5%8B%E8%AF%95"><span class="toc-number">1.0.4.</span> <span class="toc-text">准确率测试</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%BB%84"><span class="toc-number">1.0.5.</span> <span class="toc-text">分组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%93%E6%9E%9C"><span class="toc-number">1.0.6.</span> <span class="toc-text">结果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%93%E8%AE%BA"><span class="toc-number">1.0.7.</span> <span class="toc-text">结论</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%94%B9%E8%BF%9B%E7%82%B9"><span class="toc-number">1.0.8.</span> <span class="toc-text">改进点</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/01/19/redis-cluster/" title="redis-cluster"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="redis-cluster"/></a><div class="content"><a class="title" href="/2022/01/19/redis-cluster/" title="redis-cluster">redis-cluster</a><time datetime="2022-01-19T13:23:30.000Z" title="发表于 2022-01-19 21:23:30">2022-01-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/01/17/redis%E9%9B%86%E7%BE%A4/" title="redis集群"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="redis集群"/></a><div class="content"><a class="title" href="/2022/01/17/redis%E9%9B%86%E7%BE%A4/" title="redis集群">redis集群</a><time datetime="2022-01-17T07:08:35.000Z" title="发表于 2022-01-17 15:08:35">2022-01-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/31/%E5%B7%A5%E4%B8%9A%E4%BA%92%E8%81%94%E7%BD%91%E7%AB%AF%E8%BE%B9%E4%BA%91%E5%8D%8F%E5%90%8C%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5%E6%96%B9%E6%A1%88%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%AE%9E%E7%8E%B0%E8%AE%BA%E6%96%87%E7%AC%94%E8%AE%B0/" title="工业互联网端边云协同数据同步方案设计与实现论文笔记"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="工业互联网端边云协同数据同步方案设计与实现论文笔记"/></a><div class="content"><a class="title" href="/2021/12/31/%E5%B7%A5%E4%B8%9A%E4%BA%92%E8%81%94%E7%BD%91%E7%AB%AF%E8%BE%B9%E4%BA%91%E5%8D%8F%E5%90%8C%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5%E6%96%B9%E6%A1%88%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%AE%9E%E7%8E%B0%E8%AE%BA%E6%96%87%E7%AC%94%E8%AE%B0/" title="工业互联网端边云协同数据同步方案设计与实现论文笔记">工业互联网端边云协同数据同步方案设计与实现论文笔记</a><time datetime="2021-12-31T08:47:17.000Z" title="发表于 2021-12-31 16:47:17">2021-12-31</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/31/RSYNC/" title="RSYNC"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="RSYNC"/></a><div class="content"><a class="title" href="/2021/12/31/RSYNC/" title="RSYNC">RSYNC</a><time datetime="2021-12-31T07:36:41.000Z" title="发表于 2021-12-31 15:36:41">2021-12-31</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/17/%E7%AE%97%E5%8A%9B%E8%B5%84%E6%BA%90%E4%B8%8E%E7%8A%B6%E6%80%81%E6%9C%8D%E5%8A%A1%E4%BF%A1%E6%81%AF%E7%9A%84%E5%8E%8B%E7%BC%A9%E7%AE%97%E6%B3%95%E8%B0%83%E7%A0%94/" title="算力资源与状态服务信息的压缩算法调研"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="算力资源与状态服务信息的压缩算法调研"/></a><div class="content"><a class="title" href="/2021/12/17/%E7%AE%97%E5%8A%9B%E8%B5%84%E6%BA%90%E4%B8%8E%E7%8A%B6%E6%80%81%E6%9C%8D%E5%8A%A1%E4%BF%A1%E6%81%AF%E7%9A%84%E5%8E%8B%E7%BC%A9%E7%AE%97%E6%B3%95%E8%B0%83%E7%A0%94/" title="算力资源与状态服务信息的压缩算法调研">算力资源与状态服务信息的压缩算法调研</a><time datetime="2021-12-17T08:34:12.000Z" title="发表于 2021-12-17 16:34:12">2021-12-17</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By wrj</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>